#!/bin/bash
#
# Copyright (c) 2011 Federico Ch. Tomasczik (LW3ESH)
# E-Mail: <ftomasczik at gmail dot com>
#
# savemeqsl (Save Me eQSL) is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published 
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# savemeqsl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with savemeqsl.  If not, see <http://www.gnu.org/licenses/>.
#
VERSION=0.1.0

TMP="/var/tmp"
URL="http://www.eqsl.cc/qslcard/"


# Prints the version number:
function printVersion
{
	echo $"Save Me eQSL: savemeqsl version $VERSION."
	echo $"Simple bash script for download yours Electronic QSL cards from eQSL.cc site."
	echo ""
	echo $"Latest version is at :" 
	echo '		http://code.google.com/p/s3-bash/'
	echo ""
	echo "				Federico Ch. Tomasczik, LW3ESH."
}



# Print help and exit:
function printHelpAndExit
{
	exitCode=$1
	echo $" Usage $0: [-hvoVq] -u callsign -p passwrd -d directory."
	echo $"  -h, --help print this help."
	echo $"  -v, --version show version."
	echo $"  -o, overwrite ALL eQSL files."
	echo $"  -V, --verbose show detailed information."
	echo $"  -q, --quiet don't show any information."
	echo $"  -u your callsign or user in eQSL.cc."
	echo $"  -p your password in eQSL.cc."
	echo $"  -d directory where save yours eQSL."
	exit $exitCode
}

function showErrorAndExit
{
	exitCode=$2
	printf "$1"
	exit $exitCode
}

function printErrorHelpAndExit
{
	echo "$weAreKnownAs" "$1"
	printHelpAndExit $2
}

function parseOptions
{
	while getopts "hvVo:u:p:d:q-help-verbose-version-quiet" OPTION; do		
		case "$OPTION" in
			h|--help)	printHelpAndExit 0;;
			v|--version)	printVersion;exit 0;;
			V|--verbose)	VERBOSE="Y";;
			q|--quiet)	QUIET="Y";;
			o)	OVERWRITE="Y";;
			u)	USER=$OPTARG;;
			p)	PASS=$OPTARG;;
			d)	DESTINATION=$OPTARG
				if [ ! -d $DESTINATION ];then
					showErrorAndExit $"The directory $DESTINATION does not exist!\nCheck this and try again.\n" 20
				fi;;
#			#-*)	showErrorAndExit "Option not recognised.\nTry '$0 -h´ for more information.\n" 21;;
			[?])	showErrorAndExit "Option not recognised.\nTry '$0 -h´ for more information.\n" 21;;
		esac
	done
#	shift $((OPTIND-1))
}

parseOptions "$@"

if [ ! "$QUIET" ];then
	echo $" Connecting to eQSL site..."
fi

wget -T 5 --cookies=on --keep-session-cookies --save-cookies="$TMP/cookie.txt" -O "$TMP/tmp.txt" $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go 2>/dev/null

if [ ! "$QUIET" ];then
	echo $" Login to eQSL site..."
fi

wget -q -T 5 --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies --save-cookies="$TMP/cookie.txt" -O "$TMP/tmp.txt" $URL"DownloadInBox.cfm" 2>/dev/null


# if file tmp.txt contain 'You are not yet logged in' then...
t=`grep "You are not yet logged in" $TMP/tmp.txt`
if [ -n "$t" ];then
	showErrorAndExit $"User or password are incorrect.\nPlease, try again.\n" 21
fi

t=`grep ".TXT file" "$TMP/tmp.txt" | cut -d '"' -f2`

if [ ! "$QUIET" ];then
	echo $" Downloading you logbook file..."
fi

wget -q -T 5 --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies -O "$TMP/log.txt" $URL$t 2>/dev/null

if [ ! "$QUIET" ];then
	echo $" Reading you logbook file..."
fi

grep "<CALL:" "$TMP/log.txt" | while read LINE
do
CALLSIGNAL=$(echo $LINE | cut -d '<' -f2 | cut -d '>' -f2)
DATE=$(echo $LINE | cut -d '<' -f3 | cut -d '>' -f2)
TIME=$(echo $LINE | cut -d '<' -f4 | cut -d '>' -f2)
BAND=$(echo $LINE | cut -d '<' -f5 | cut -d '>' -f2)
MODE=$(echo $LINE | cut -d '<' -f6 | cut -d '>' -f2)

FILE_NAME=$CALLSIGNAL"_"$DATE"_"$TIME"_"$BAND"_"$MODE".jpg"
FILE_NAME=$(echo $FILE_NAME|sed 's/\//-/g')

TIME="$(expr substr $TIME 1 2)":"$(expr substr $TIME 3 2)"
DATE="$(expr substr $DATE 1 4)"-"$(expr substr $DATE 5 2)"-"$(expr substr $DATE 7 2)"

#if [ $OVERWRITE ne "Y" ];then


	if [ -f $DESTINATION"/"$FILE_NAME ];then
		echo $" The $FILE_NAME exists. No overwrite it."
		else


if [ ! "$QUIET" ];then
	echo $" Downloading eQSL for you QSO with $CALLSIGNAL..."
fi

wget -q -T 25 --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies -O "$TMP/tmp.txt" $URL"DisplayQSL.cfm?Callsign="$CALLSIGNAL"&VisitorCallsign="$USER"&QSODate="$DATE"%20"$TIME"&Band="$BAND"&Mode="$MODE 2>/dev/null

t=`grep "TempeQSLs" "$TMP/tmp.txt" | cut -d '"' -f2 | cut -d '/' -f4`

wget -q -T 25 --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies -O $DESTINATION"/"$FILE_NAME $URL"TempeQSLs/"$t 

	fi

done


rm "$TMP/cookie.txt"
rm "$TMP/tmp.txt"
rm "$TMP/log.txt"
exit 0
